Oa.controller('meetingCtrl',function($scope,$http){
	$scope.showModal = false;
	$scope.meeting = {};
	$('.date-picker').datepicker();
	$('.time-picker').timepicker();

	$scope.addMeetingBtn = function(){
		$scope.showModal = !$scope.showModal;
	}
	$scope.sureaddMeeting = function(){
		$scope.meeting.members = $("#e2").val();
		$scope.meeting.name = sessionStorage.username;
		$scope.meeting.time =$("#date").val()+" "+$scope.time;
	
		$http.post(addMeeting_url,$scope.meeting)
		.success(function(res){
			if (res.code==200) {
				Notify(res.msg,"top-right");
				$scope.showModal = !$scope.showModal;
				$scope.getMyMeeting(); 
				//$scope.calendr();
				location.reload();
			}else{
				Notify(res.data,"top-right");
			}
		})
		.error(function(){
			Notify("无网络，请稍后重试","top-right");
		});
	}

	$scope.showModal2 = false;
	$scope.toggleshowModal2 = function(){
		$scope.showModal2 = !$scope.showModal2;
	}
	$scope.showMeetingDetails = function() {
		$scope.showModal2 = true;
	}

	$http.get(getAllUser_url,
		{
			params:{
				page:1,
				size:10000
			}
		})
			.success(function(res){
				if (res.code==200) {
					$scope.users = res.data.list;
					
				}else {
					Notify(res.msg,"top-right");
				}
			}).error(function(){
				Notify('客户端错误',"top-right");
	});

	$scope.calendr = function(){
	$('#calendar').fullCalendar({
		  header: {
		      right: 'month,agendaWeek,agendaDay prev,next',
		      left: 'title'
		  },
		  editable: true,
		  buttonText: {
		      prev: '<i class="fa fa-chevron-left"></i>',
		      next: '<i class="fa fa-chevron-right"></i>',
		      today: '今天',
		      month: '月',
		      week: '周',
		      day: '日'
		  },
		//  droppable: true,
		  drop: function (date, allDay) {
		      var originalEventObject = $(this).data('eventObject');
		      var copiedEventObject = $.extend({}, originalEventObject);
		      copiedEventObject.start = date;
		      copiedEventObject.allDay = allDay;
		      $('#calendar').fullCalendar('renderEvent', copiedEventObject, true);
		      if ($('#drop-remove').is(':checked')) {
		          $(this).remove();
		      }
		  },
			dayClick:function(date,allDay,jsEvent,view) {
				
				var mydate = new Date(date);
				console.log(mydate);
				//console.log(jsEvent);
				$("#add-meeting-btn").click();
				$("#date").val(mydate.toLocaleDateString());
			},
			eventClick: function(event, jsEvent, view) {
				// $scope.showMeetingDetails(event);
				$scope.event = event;
				$("#cl").click();
				
			},
			events:function(start,end,callback){
				$http.get(getAllMeeting_url)
				.success(function(res){
					var meetings = res.data;
					var events = [];
					for(var i=0;i<meetings.length;i++){
						meetings[i].start = new Date(meetings[i].time);
						var str = meetings[i].start.toDateString();
						
						var istoday = $scope.isToday(str);
						
						if(istoday==0){ //今天
							meetings[i].borderColor = '#fb6e52';
							if($scope.mymeeting.indexOf(meetings[i].id)!=-1){
								console.log("有我")
								meetings[i].borderColor =  '#ed4e2a';
								//meetings[i].backgroundColor = "#2dc3e8";
							}
						}
						else if(istoday==1){ //未来
							meetings[i].borderColor = '#2dc3e8';
							if($scope.mymeeting.indexOf(meetings[i].id)!=-1){
								console.log("有我")
								meetings[i].borderColor =  '#ed4e2a';
							//	meetings[i].backgroundColor = "#2dc3e8";
							}
						}
						events.push(meetings[i]);
					}
					callback(events);
				})
				.error(function(){

				});
			}		
	});
	}
	$scope.isToday = function(str){
	    var d = new Date(str.replace(/-/g,"/"));
	    var todaysDate = new Date();
	    if(d.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)){
	        return 0;	//今天
	    } else if(d.setHours(0,0,0,0) > todaysDate.setHours(0,0,0,0)){
	        return 1;  //未来
	    }else if(d.setHours(0,0,0,0) < todaysDate.setHours(0,0,0,0)){
	    	return 2; //过去
	    }
	}
	$scope.mymeeting = [];
	$scope.getMyMeeting = function(){
		$http.get(getMyMeeting_url,{
			params:{
				userId:sessionStorage.userid
			}
		})
		.success(function(res){
			if(res.code==200){
				for(var i=0;i<res.data.length;i++){
					$scope.mymeeting.push(res.data[i].id);
				}
				
			}
		})
		.error(function(){
			
		});
	}
	
	$scope.getMyMeeting(); 
  $scope.calendr();
});
